package com.version1_3.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.util.format.MapToJson;
import com.version1_1.service.SubscriService;
import com.version1_3.service.SubscriService1_3;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;

/**
 * Created by kuibu on 15/12/25.
 */

@Controller
public class SubscriController1_3 {
    private static final Logger LOGGER = Logger.getLogger(SubscriController1_3.class);

    @Autowired
    private SubscriService1_3 subscriService13;

    @Autowired
    private SubscriService subscriService;


    /**
     * 调用接口: http://www.kuibutech.com/kuibuversion1/version1_3/subscri/changeUserFocus/{userId}
     * <p>
     * 在订阅界面更新用户关注分类内容或者给订阅的三级分类排序  - 目前iOS订阅功能只用了这一个接口
     */
    @RequestMapping(value = "/version1_3/subscri/changeUserFocus/{userId}", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String changeUserFocus(@RequestBody JSONObject jsonObject, @PathVariable("userId") int userId) {

        JSONArray updateFocusJsonArray = jsonObject.getJSONArray("updateFocusStr");
        if (updateFocusJsonArray == null) {
            LOGGER.error("没有接收到客户端发送数据");
            return "{\"code\":500}";
        } else {
            return subscriService13.changeUserFocus(updateFocusJsonArray, userId);
        }
    }

    /**
     * 调用接口：http://www.kuibutech.com/kuibuversion1/version1_3/subscri/subscriThirdType/{userId}/{firstType}{thirdTypeId}
     *
     * @Description: 订阅单独的三级分类
     */
    @RequestMapping(value = "/version1_3/subscri/subscriThirdType/{userId}/{firstType}/{thirdTypeId}", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String subscriThirdType(@PathVariable("userId") int userId, @PathVariable("thirdTypeId") int thirdTypeId, @PathVariable("firstType") int firstType) {

        return new MapToJson("subscriResult", subscriService13.subscriThirdType(userId, firstType, thirdTypeId)).toJsonString();
    }


    /**
     * 调用接口：http://www.kuibutech.com/kuibuversion1/version1_3/subscri/removeSubscri/{userId}/{firstType}/{thirdTypeId}
     *
     * @Description: 取消订阅单独的三级分类
     */
    @RequestMapping(value = "/version1_3/subscri/removeSubscri/{userId}/{firstType}/{thirdTypeId}", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String removeSubscri(@PathVariable("userId") int userId, @PathVariable("firstType") int firstType, @PathVariable("thirdTypeId") int thirdTypeId) {

        return new MapToJson("removeSubscriResult", subscriService13.removeSubscri(userId, firstType, thirdTypeId)).toJsonString();
    }


    /*------------------------------------------------- version1.2 接口  ---------------------------------------------------------*/

    /**
     * 调用接口：http://www.kuibutech.com/kuibuversion1/subscri/getHotSortation/{number}
     *
     * @Description: 获取热门分类
     */
    @RequestMapping(value = "/version1_3/subscri/getHotSortation/{number}", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String getHotSortation(@PathVariable("number") int number) throws UnsupportedEncodingException {
        return subscriService.getHotSortation(number);
    }
}
